package com.app.asktom;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.app.asktom.abs.AbsActivity;
import com.app.asktom.component.DeviceHelper;
import com.app.asktom.model.User;

public class MainActivity extends AbsActivity {

	public static int counter = 1;
	public ProgressDialog dialog;
	
	public void setLayout(int layout) {
		setContentView(layout);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.loading);
		
		EditText txtPhoneNo = (EditText) 
				findViewById(R.id.txtPhoneNumber);
		String userPhoneNo = DeviceHelper.getUserPhoneNumber(this); 
		
		// auto set user phone number
		if(!userPhoneNo.equals("no number")) {
			txtPhoneNo.setText(userPhoneNo, 
				TextView.BufferType.EDITABLE);		
		}
		

		if(!DeviceHelper.hasConnection(this)) {
			showAlertDialog("Internet Connection", "No Internet Connection", false);
		} else {
			Intent intent = null;
			User user = new User(this);
			if(user.isUserActivated()) {
				intent = new Intent(this,ViewFoodActivity.class);
//				intent = new Intent(this,TestActivity.class);
			} else {
				if(user.isUserRegistered()){
					intent = new Intent(this,ActivationActivity.class);
				} else {
					intent = new Intent(this,RegisterActivity.class);
				}
			}
			startActivity(intent);	
			this.finish();
		}
	}	
	
	@Override
	public void onClickAlertOKButton() {
		this.finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case R.id.menu_quit:
				this.finish();
				return true;
			default:
				return super.onOptionsItemSelected(item);
		}
	}
	
}
